package com.xauto.pioneer.appuser.model;

import android.content.Context;

import com.xauto.pioneer.appuser.util.DbUtil;

import net.tsz.afinal.FinalDb;
import net.tsz.afinal.annotation.sqlite.Id;
import net.tsz.afinal.annotation.sqlite.Table;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * 支付方式信息
 * Created by Lemon on 16/4/25.
 */
@Table(name = "table_payment")
public class Payment extends BaseModel<Payment> {

    /** 支付方式标识*/
    @Id(column = "payment_code")
    public String payment_code;
    /** 支付方式名称*/
    public String payment_name;
    /** 支付方式icon*/
    public Image payment_image;
    /** 支付方式 缩略图icon*/
    public String payment_image_thumb;
    /** 支付方式描述*/
    public String payment_description;
    /** 显示选项(1-是/0-否)*/
    public String is_show;
    /** 当前是否选中*/
    public boolean isChoose = false;

    public String getPayment_code() {
        return payment_code;
    }

    public void setPayment_code(String payment_code) {
        this.payment_code = payment_code;
    }

    public String getPayment_name() {
        return payment_name;
    }

    public void setPayment_name(String payment_name) {
        this.payment_name = payment_name;
    }

    public Image getPayment_image() {
        return payment_image;
    }

    public void setPayment_image(Image payment_image) {
        this.payment_image = payment_image;
    }

    public String getPayment_description() {
        return payment_description;
    }

    public String getPayment_image_thumb() {
        return payment_image_thumb;
    }

    public void setPayment_image_thumb(String payment_image_thumb) {
        this.payment_image_thumb = payment_image_thumb;
    }

    public boolean isChoose() {
        return isChoose;
    }

    public void setChoose(boolean choose) {
        isChoose = choose;
    }

    public void setPayment_description(String payment_description) {
        this.payment_description = payment_description;
    }

    public String getIs_show() {
        return is_show;
    }

    public void setIs_show(String is_show) {
        this.is_show = is_show;
    }

    @Override
    public Payment parse(JSONObject jsonObject) {
        if (jsonObject != null) {
            payment_code = jsonObject.optString("payment_code");
            payment_name = jsonObject.optString("payment_name");
            payment_image = new Image().parse(jsonObject.optJSONObject("payment_image"));
            if (payment_image != null)payment_image_thumb = payment_image.thumb;
            payment_description = jsonObject.optString("payment_description");
            is_show = jsonObject.optString("is_show");
            return this;
        }
        return null;
    }

    /** 获取当前上线的支付方式*/
    public List<Payment> getPaymentListForOnLine(Context mContext) {
        List<Payment> payments = DbUtil.getInstance(mContext).getDataList(Payment.class);
        List<Payment> showPayments = new ArrayList<>();
        if(payments != null && payments.size() > 0) {
            for (int i = 0; i < payments.size(); i++) {
                if(!payments.get(i).getIs_show().equals("0")) {
                    payments.get(i).isChoose = false;
                    if(payments.get(i).getPayment_code().equals("app-alipay")) {
                        //默认使用支付宝支付
                        payments.get(i).isChoose = true;
                    }
                    showPayments.add(payments.get(i));
                }
            }
        }
        return showPayments;
    }

    public void saveDb(Context mContext, Payment payment) {
        FinalDb finalDb = FinalDb.create(mContext);
        finalDb.deleteByWhere(Payment.class, "payment_code = \""+payment.payment_code+"\"");
        finalDb.save(payment);
    }
}
